package com.woc.boot.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.woc.boot.admin.web.LoginInterceptor;

/**
 * web层配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(getLoginInterceptor())
            // 管理页面
            .addPathPatterns("/")
            .addPathPatterns("/data/**")
            .addPathPatterns("/domain/**")
            .addPathPatterns("/email/**")
            .addPathPatterns("/function/**")
            .addPathPatterns("/host/**")
            .addPathPatterns("/role/**")
            .addPathPatterns("/seo/**")
            .addPathPatterns("/site/**")
            .addPathPatterns("/type/**")
            .addPathPatterns("/user/**")
            // 对静态资源设置匿名访问
            .excludePathPatterns("/favicon.ico**")
            .excludePathPatterns("/style/**")
            .excludePathPatterns("/font/**")
            .excludePathPatterns("/fonts/**")
            .excludePathPatterns("/js/**")
            // 对接口设置匿名访问
            .excludePathPatterns("/auth/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedMethods("GET", "POST", "PUT", "DELETE")//设置允许的方法
                .maxAge(3600);//跨域允许时间
    }
}
